/* Copyright (c) 2007 Jan Galinski
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.ascrblr.cli.conf;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Properties;

import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;


/**
 * The Enum CommandLineOption.
 */
public enum CommandLineOption {

	/** The LASTF m_ USER. */
	LASTFM_USER(createOption("u", "user", true, false)),
	
	/** The LASTF m_ PASSWD. */
	LASTFM_PASSWD(createOption("p", "password", true, false)),
	
	/** The LASTF m_ Recursive. */
	RECURSIVE(createOption("r", "recursive", false, false)),
	
	/** The LASTF Help. */
	HELP(createOption("h", "help", false, false)),
	
	/** The LASTF m_ Date. */
	DATE(createOption("d", "date", true, false));

	/** The option. */
	private Option option = null;

	/** The localization. */
	private static Properties localization = null;
	
	/** The Constant HF. */
	private static final HelpFormatter HF = new HelpFormatter();

	/**
	 * Gets the localization.
	 * 
	 * @return the localization
	 */
	public static Properties getLocalization() {
		if (localization == null) {
			initLocalization();
		}
		
		return localization;
	}

	/**
	 * Instantiates a new command line options.
	 * 
	 * @param option the option
	 */
	private CommandLineOption(Option option) {
		if (option == null) {
			throw new IllegalArgumentException(
					"CommandLineOption(): option is null.");
		}
		this.option = option;
	}

	/**
	 * Gets the option.
	 * 
	 * @return the option
	 */
	public Option getOption() {
		return option;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Enum#toString()
	 */
	@Override
	public String toString() {
		return new StringBuffer().append(super.toString()).append("{").append(
				"option=" + option).append("}").toString();
	}

	/**
	 * Gets the options.
	 * 
	 * @return the options
	 */
	public static Options getOptions() {
		ArrayList<CommandLineOption> list = new ArrayList<CommandLineOption>(
				Arrays.asList(values()));
		Options options = new Options();
		for (Iterator<CommandLineOption> it = list.iterator(); it.hasNext();) {
			options.addOption(it.next().getOption());
		}
		return options;
	}

	/**
	 * Creates the option.
	 * 
	 * @param shortOption the short option
	 * @param longOption the long option
	 * @param hasArg the has arg
	 * @param required the required
	 * 
	 * @return the option
	 */
	private static Option createOption(String shortOption, String longOption,
			boolean hasArg, boolean required) {

		String description = getLocalization().getProperty(
				"cli.option.message." + longOption);
		Option option = new Option(shortOption, longOption, hasArg, description);
		
		option.setRequired(required);
		
		return option;
	}

	/**
	 * Gets the localization.
	 * 
	 * @return true if init succeeded
	 */
	private static boolean initLocalization() {
		if (localization != null && !localization.isEmpty()) {
			return false;
		}

		localization = new Properties();

		try {
			InputStream locIps = CommandLineOption.class
					.getResourceAsStream("/localization.properties");
			localization.load(locIps);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}
	
	
	/**
	 * Prints the usage.
	 * 
	 * @return the string
	 */
	public static String getUsage() {
		return getUsage("");
	}

	/**
	 * Prints the usage.
	 * 
	 * @param message the message
	 * 
	 * @return the string
	 */
	public static String getUsage(String message) {
		message = (message == null || "".equals(message.trim())) ? "" : message + HF.getNewLine() + HF.getNewLine(); 
		

		String header = getLocalization()
				.getProperty("cli.option.usage.header");
		String footer = getLocalization()
				.getProperty("cli.option.usage.footer");

		StringWriter usageLine = new StringWriter();
		HF.printUsage(new PrintWriter(usageLine), 80, "scrobbler",
				getOptions());
		String usage = usageLine.toString().replaceFirst(
				HF.getSyntaxPrefix(), "").replaceFirst(HF.getNewLine(), " path1 ... pathN " + HF.getNewLine());

		StringWriter sw = new StringWriter();
				HF.printHelp(new PrintWriter(sw), 80, usage, header + HF.getNewLine(),
				getOptions(), 10, 10, HF.getNewLine() + footer );

		return   message + sw.toString();
	}

}
